<template>
	<view>
		<view class="tab">
			<view class="tabItem" :class="{'tabItem_active':tabIndex == index}" v-for="(item,index) in tabList"
				:key="index" @tap="switchTab(index)">{{item}}</view>
		</view>
		<view class="none" v-if="isPage && couponList.length == 0">
			<i class="iconfont iconquesheng"></i>
			<view v-if="tabIndex == 1">还没有可使用的优惠券<br/>快去<text @tap.stop="switchTab(0)">领券中心</text>看看吧~</view>
			<text v-else>暂无数据~</text>
		</view>
		<view class="list" v-else>
			<view class="item" v-for="(item,index) in couponList" :key="index">
				<discount-coupon :itemCont="item" :itemIndex="index" @event="getDiscount"></discount-coupon>
			</view>
			<uni-load-more v-if="couponList.length >= 8" :status="status" :content-text="contentText" />
		</view>
	</view>
</template>

<script>
	import discountCoupon from '@/components/discount-coupon/discount-coupon.vue';
	export default {
		components: {
			discountCoupon
		},
		data() {
			return {
				tabIndex: 0, //选项卡索引
				tabList: ['领券中心', '可使用', '已使用', '已失效'], //选项卡列表
				couponList: [], //优惠券列表
				page: 1,
				reload: true,
				isPage: false,
				status: 'more',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多',
					contentnot: "没数据"
				}
			}
		},
		onPullDownRefresh() {
			this.publicFunc();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
			this.status = 'loading';
			this.page = this.page + 1;
			var _this = this;
			setTimeout(function() {
				_this.getList();
			}, 500);
		},
		onShow() {
			this.publicFunc();
		},
		methods: {
			// 获取优惠券列表
			getList() {
				var data = {
					page: this.page
				};
				var url = 'font/coupon/infos';
				if (this.tabIndex > 0) {
					data.type = this.tabIndex;
					url = 'font/coupon/my';
				}
				this.$shopApi.apiPost(url, data).then(res => {
					if (res.data.code == 200 && res.data.data.list && res.data.data.list.length > 0) {
						var list = res.data.data.list;
						list.forEach(item => {
							item['state'] = this.tabIndex; //自定义优惠券状态
							var myReceive = Number(item.quota - item.myreceive); //优惠券剩余领取次数
							if (this.tabIndex == 0 && myReceive <= 0) {
								item['state'] = -1;
							}
							item['stateName'] = this.util.couponState(item.state);
							item['number'] = Number(item.total - item.receive); //优惠券剩余数量
							item.startTime = item.start_time.substring(0, 10).replace(/-/g, '.');
							item.stopTime = item.stop_time.substring(0, 10).replace(/-/g, '.');
						})
						this.$forceUpdate();
						this.couponList = this.reload ? list : this.couponList.concat(list);
						this.reload = false;
						this.status = 'more';
					} else {
						if (this.reload) {
							this.couponList = [];
							this.isPage = true;
						} else {
							this.status = 'noMore';
						}
					}
				})
			},
			// 领取优惠券
			getDiscount(e) {
				this.$shopApi.apiPost('font/coupon/get', {
					unid: e.unid
				}).then(res => {
					this.util.showDialog(res.data.message);
					if (res.data.code == 200) {
						this.couponList[e.index].myreceive++; //个人领取优惠券次数加1
						this.couponList[e.index].receive++; //整体优惠券被领取次数加1
						this.couponList[e.index]['number'] = Number(this.couponList[e.index].total - this.couponList[e.index].receive); //优惠券剩余数量
						var myReceive = Number(this.couponList[e.index].quota - this.couponList[e.index].myreceive); //优惠券剩余领取次数
						if (this.tabIndex == 0 && myReceive <= 0) {
							this.couponList[e.index]['state'] = -1;
						}
						this.couponList[e.index]['stateName'] = this.util.couponState(this.couponList[e.index].state);
						this.$forceUpdate();
					}
				})
			},
			// 切换选项卡
			switchTab(index) {
				if (this.tabIndex != index) {
					this.tabIndex = index;
					this.publicFunc();
				}
			},
			// 公用方法
			publicFunc() {
				this.page = 1;
				this.reload = true;
				this.isPage = false;
				this.couponList = [];
				this.getList();
			}
		}
	}
</script>

<style>
	page {
		background: #FAFAFA;
	}

	.list {
		padding: 96upx 0;
	}

	.item {
		width: 660upx;
		margin: 20upx auto 40upx;
		position: relative;
	}
</style>
